package com.example.readis;

import com.example.home.StringEmpty;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Component;

@Component
public class RedisUtil {
    @Autowired
    private RedisTemplate<String,Object>redisTemplate;
    @Resource(name = "redisTemplate")
    private HashOperations<String,String,Object> hashOperations;
    @Resource(name = "redisTemplate")
    private ListOperations<String,Object> listOperations;
    @Resource(name = "redisTemplate")
    private ValueOperations<String,Object> valueOperations;
    @Resource(name = "redisTemplate")
    private SetOperations<String,Object> setOperations;
    @Resource(name = "redisTemplate")
    private ZSetOperations<String,Object> zSetOperations;
    public RedisUtil(RedisTemplate<String, Object>redisTemplate) {
        this.redisTemplate = redisTemplate;
    }
    public boolean has_key(String key){
        try {
            return redisTemplate.hasKey(key);
        }catch (Exception error){
            error.printStackTrace();
            return  false;
        }
    }
    public boolean set(String key,Object object){
        try {
            redisTemplate.opsForValue().set(key,object);
            return true;
        }catch (Exception error){
            error.printStackTrace();
            return false;
        }
    }
    public Object get(String key){
        return StringEmpty.isNULL(key) ? null : redisTemplate.opsForValue().get(key);
    }
    public boolean delete(String key){
        return StringEmpty.isNULL(key) ? false : redisTemplate.delete(key);
    }

}
